﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using com.paypal.sdk.core;
using com.paypal.sdk.profiles;
using com.paypal.sdk.services;
using com.paypal.sdk.util;

namespace DigitalGoodsExample
{
    public partial class DirectPayment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         if (!this.IsPostBack)
            {
                creditCardNumber.Value = Util.GenerateCreditCardNumber(creditCardType.SelectedItem.Value);
            }
        }

        protected void PayButton_Click(object sender, System.EventArgs e)
        {
            NVPCallerServices caller = new NVPCallerServices();
            IAPIProfile profile = ProfileFactory.createSignatureAPIProfile();

            /*WARNING: Do not embed plaintext credentials in your application code.
            Doing so is insecure and against best practices.Your API credentials must 
            be handled securely. Please considerencrypting them for use in any production 
            environment, and ensurethat only authorized individuals may view or modify them.*/

            // Set up your API credentials, PayPal end point, API operation and version.profile.
            profile.APIUsername = Constants.API_USERNAME;
            profile.APIPassword = Constants.API_PASSWORD;
            profile.APISignature = Constants.API_SIGNATURE;
            profile.Environment = Constants.ENVIRONMENT;
            caller.APIProfile = profile;
            NVPCodec encoder = new NVPCodec();

            encoder["VERSION"] = "65.1";

            //com.paypal.sdk.services.NVPCallerServices caller = PayPalAPI.PayPalAPIInitialize();
            //NVPCodec encoder = new NVPCodec();
            encoder["METHOD"] = "DoDirectPayment";
            encoder["PAYMENTACTION"] = this.Request.QueryString[Constants.PAYMENT_TYPE_PARAM];
            encoder["AMT"] = amount.Value;
            encoder["CREDITCARDTYPE"] = creditCardType.SelectedItem.Value;
            encoder["ACCT"] = creditCardNumber.Value;
            encoder["EXPDATE"] = expdate_month.Value.ToString() + expdate_year.Value;
            encoder["CVV2"] = cvv2Number.Value;
            encoder["FIRSTNAME"] = firstName.Value;
            encoder["LASTNAME"] = lastName.Value;
            encoder["STREET"] = address1.Value;
            encoder["CITY"] = city.Value;
            encoder["STATE"] = state.Value;
            encoder["ZIP"] = zip.Value;
            encoder["COUNTRYCODE"] = "US";
            encoder["CURRENCYCODE"] = currency.Value;

            string pStrrequestforNvp = encoder.Encode();
            string pStresponsenvp = caller.Call(pStrrequestforNvp);

            NVPCodec decoder = new NVPCodec();
            decoder.Decode(pStresponsenvp);

            string strAck = decoder["ACK"];
            if (strAck != null && (strAck == "Success" || strAck == "SuccessWithWarning"))
            {
                Session["result"] = decoder;
                string pStrResQue = "API=" + "DoDirect Payment ";
                Response.Redirect("DoDirectPaymentReceipt.aspx?" + pStrResQue);
            }
            else
            {
                Session["errorresult"] = decoder;
                string pStrResQue = "API=" + "Error Detail ";
                Response.Redirect("APIError.aspx?" + pStrResQue);
            }
        }

        protected void creditCardType_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            creditCardNumber.Value = Util.GenerateCreditCardNumber(creditCardType.SelectedItem.Value);
        }
    }
}
